/**
 * Copyright(c) 2013 Jade Techonologies Co., Ltd.
 *
 * History:
 *   13-4-14 下午4:00 Created by dwzhang
 */
package com.jade.framework.base.criterion;

import java.io.Serializable;

/**
 * Represents an strategy for matching strings using "like".
 *
 * @author dwzhang
 * @version 1.0 13-4-14 下午4:00
 */
public abstract class MatchMode implements Serializable
{
    private final String name;

    protected MatchMode(String name)
    {
        this.name = name;
    }

    public String toString()
    {
        return name;
    }

    /** Match the entire string to the pattern */
    public static final MatchMode EXACT = new MatchMode("EXACT")
    {
        public String toMatchString(String pattern)
        {
            return pattern;
        }
    };

    /** Match the start of the string to the pattern */
    public static final MatchMode START = new MatchMode("START")
    {
        public String toMatchString(String pattern)
        {
            return pattern + '%';
        }
    };

    /** Match the end of the string to the pattern */
    public static final MatchMode END = new MatchMode("END")
    {
        public String toMatchString(String pattern)
        {
            return '%' + pattern;
        }
    };

    /** Match the pattern anywhere in the string */
    public static final MatchMode ANYWHERE = new MatchMode("ANYWHERE")
    {
        public String toMatchString(String pattern)
        {
            return '%' + pattern + '%';
        }
    };

    /**
     * convertGroup the pattern, by appending/prepending "%"
     *
     * @param pattern convertGroup pattern
     * @return the match string.
     */
    public abstract String toMatchString(String pattern);
}
